public class MyLinkedList implements IList{
    public void creatList() {
        ListNode Node1 = new ListNode(12);
        ListNode Node2=  new ListNode(23);
        ListNode Node3 = new ListNode(34);
        ListNode Node4=  new ListNode(45);
        Node1.next = Node2;
        Node2.next = Node3;
        Node3.next = Node4;
        Node4.next = null;
        this.head = Node1;
    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;

    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null) {
            this.head = node;
        } else {
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        if(cur == null) {
            return 0;
        }
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode Node = new ListNode(data);
        if (index < 0 || index > size()) {
            throw new IndexException("index为异常");

        }
        if(size() == 0) {
           this.head = Node;
           return;
        }
        if(index == 0) {
           addFirst(data);
           return;
        }
        if(size() == index) {
            addLast(data);
            return;
        }
        ListNode cur = findcur(index);
        Node.next = cur.next;
        cur.next = Node;
    }
    public ListNode findcur(int index) {
        ListNode cur = head;
        int count =  0;
        while(count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        if(head.val == key) {
            head = head.next;
        }
        if(head == null) {
            return;
        }
        while (cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                break;
            } else {
                cur = cur.next;
            }
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head.next;
        ListNode prevcur = head;
        if(head == null) {
            throw new EmplyException("链表为空");
        }
        while(cur != null) {
            if(cur.val == key) {
                prevcur.next = cur.next;
                cur = cur.next;
            } else {
                prevcur = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }

    }

    @Override
    public void clear() {

    }

    @Override
    public boolean contains(int key) {
return false;
    }


    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;


    public ListNode reverseList() {
        if(head == null) {
            return null;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur !=null) {
            ListNode curnext = cur.next;
            cur.next = head;
            head = cur;
            cur = curnext;
        }
        return head;
    }
    public ListNode middleNode() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        int i = 0;
        while(i != count/2 ) {
            head =head.next;
            i++;
        }
        return head;
    }
    public void display(ListNode newhead) {
        ListNode cur = newhead;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

}

